﻿using System;
using System.Data;
using System.Windows.Forms;

namespace ZYCommon
{
   

    public class DBComboBox : ComboBox
    {
        private bool bolAutoLoad = true;
        private bool bolLoaded = false;
        private IDbConnection myConnection = null;
        private string strSQL = null;

        public void AddItem(string strValue, string strText)
        {
            ComboBoxItem item = new ComboBoxItem();
            item.Value = strValue;
            item.Text = strText;
            base.Items.Add(item);
        }

        public void LoadItems()
        {
            base.Items.Clear();
            if ((!StringCommon.isBlankString(this.strSQL) && (this.myConnection != null)) && (this.myConnection.State == ConnectionState.Open))
            {
                using (IDbCommand command = this.myConnection.CreateCommand())
                {
                    command.CommandText = this.strSQL;
                    IDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);
                    while (reader.Read())
                    {
                        ComboBoxItem item = new ComboBoxItem();
                        item.Text = reader.IsDBNull(0) ? null : reader[0].ToString();
                        if (reader.FieldCount > 1)
                        {
                            item.Value = reader.IsDBNull(1) ? null : reader[1].ToString();
                        }
                        else
                        {
                            item.Value = item.Text;
                        }
                        base.Items.Add(item);
                    }
                    reader.Close();
                    this.bolLoaded = true;
                }
            }
        }

        public bool LoadItems(string[] strItems)
        {
            if ((strItems != null) && ((strItems.Length % 2) == 0))
            {
                for (int i = 0; i < strItems.Length; i += 2)
                {
                    ComboBoxItem item = new ComboBoxItem();
                    item.Value = strItems[i];
                    item.Text = strItems[i + 1];
                    base.Items.Add(item);
                }
                this.bolLoaded = true;
                return true;
            }
            return false;
        }

        protected override void OnDropDown(EventArgs e)
        {
            if (!this.bolLoaded && this.bolAutoLoad)
            {
                this.LoadItems();
            }
            base.OnDropDown(e);
        }

        public bool AutoLoad
        {
            get
            {
                return this.bolAutoLoad;
            }
            set
            {
                this.bolAutoLoad = value;
            }
        }

        public IDbConnection Connection
        {
            get
            {
                return this.myConnection;
            }
            set
            {
                this.myConnection = value;
            }
        }

        public new string SelectedValue
        {
            get
            {
                ComboBoxItem selectedItem = base.SelectedItem as ComboBoxItem;
                if (selectedItem == null)
                {
                    return null;
                }
                return selectedItem.Value;
            }
            set
            {
                if (!this.bolLoaded)
                {
                    this.LoadItems();
                }
                foreach (ComboBoxItem item in base.Items)
                {
                    if (item.Value == value)
                    {
                        base.SelectedItem = item;
                        break;
                    }
                }
            }
        }

        public string SQL
        {
            get
            {
                return this.strSQL;
            }
            set
            {
                this.strSQL = value;
            }
        }

        private class ComboBoxItem
        {
            internal string Text;
            internal string Value;

            public override string ToString()
            {
                return ((this.Text == null) ? "<NULL>" : this.Text);
            }
        }
    }
}

